package com.nd.mobile.study.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.domain.ClassInfo;
import com.nd.mobile.study.domain.ClassInfoCatalog;
import com.nd.mobile.study.domain.ClassInfoCatalogRoot;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class ClassInfoCatalogActivity extends BaseActivity {

	private TabHost							mTabHost;
	private ViewPager						mViewPager;
	private SimpleTabsAdapter				mTabsAdapter;
	private LayoutInflater					mInflater;

	private ClassInfoCatalogRoot[]			mCatalogs;
	private View[]							mListViewContainer;
	private ListView[]						mListViews;
	private ClassInfoListAdapter[]			mListAdapters;

	private AdapterView.OnItemClickListener	mListListener;
	private Intent							mIntent2Detail;

	private ClassInfo						mClassInfo;
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.pager_tab);
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.tv_app_myclass);
		mHeaderBtnHome.setVisibility(View.VISIBLE);
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	@Override
	protected void setFieldValues() {
		super.setFieldValues();
		mInflater = LayoutInflater.from(this);
		mTabsAdapter = new ClassInfoCatalogTabsAdapter(this, mTabHost, mViewPager);
		mIntent2Detail = new Intent().setClass(this, ClassInfoDetailActivity.class);
		mListListener = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ClassInfoListAdapter adapter = (ClassInfoListAdapter) parent.getAdapter();
				ClassInfoCatalog news = adapter.getData().get(position);
				mIntent2Detail.putExtra(BundleConstants.CLASS_NEWS, news);
				startActivity(mIntent2Detail);
			}
		};
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getIntentData();
		loadRoots();
	}

	private void getIntentData() {
		mClassInfo = (ClassInfo) getIntent().getSerializableExtra(BundleConstants.CLASS_INFO);
	}

	private void loadRoots() {
		mLoading.p();
		new GetCatalogRootTask(this).execute();
	}

	private void loadList(int position) {
		new GetCatalogListTask(this).execute(position);
	}
	
	private View getTabContentView(int index) {
		if (null == mListViewContainer[index]) {
			View container = mInflater.inflate(R.layout.simple_list, null);
			ListView lv = (ListView) container.findViewById(R.id.common_lv);
			View emptyView = container.findViewById(R.id.common_lv_empty);
			lv.setEmptyView(emptyView);
			lv.setOnItemClickListener(mListListener);

			mListViewContainer[index] = container;
			mListViews[index] = lv;
		}
		return mListViewContainer[index];
	}
	
	private class GetCatalogRootTask extends SimpleAsyncTask<Void, Void, ClassInfoCatalogRoot[]> {

		public GetCatalogRootTask(Context context) {
			super(context);
		}

		@Override
		protected ClassInfoCatalogRoot[] onLoad(Void... params) throws Exception {
			return mClassInfo.getCatalogRoot(App.getInstance().getUser());
		}

		@Override
		protected void onUI(ClassInfoCatalogRoot[] result) {
			mListAdapters = new ClassInfoListAdapter[result.length];
			mListViews = new ListView[result.length];
			mListViewContainer = new View[result.length];
			mCatalogs = result;
			// pre-load num
			// mViewPager.setOffscreenPageLimit(result.length);

			String[] label = new String[result.length];
			for (int i = 0; i < label.length; i++) {
				label[i] = result[i].getName();
			}
			mTabsAdapter.setTabs(label);
			mLoading.v();
		}
	}

	private class GetCatalogListTask extends SimpleAsyncTask<Integer, Void, ClassInfoCatalog[]>{
		
		private int mIndex;
		
		public GetCatalogListTask(Context context) {
			super(context);
		}

		@Override
		protected ClassInfoCatalog[] onLoad(Integer... params) throws Exception {
			mIndex = params[0];
			return mCatalogs[mIndex].getClassInfoCatalog(App.getInstance().getUser());
		}

		@Override
		protected void onUI(ClassInfoCatalog[] result) {
			ClassInfoListAdapter adapter = mListAdapters[mIndex];
			if (null == adapter) {
				adapter = new ClassInfoListAdapter(mContext, result);
				mListAdapters[mIndex] = adapter;
				mListViews[mIndex].setAdapter(adapter);
			} else {
				adapter.setData(result);
			}
			adapter.notifyDataSetChanged();
		}
	}
	
	private class ClassInfoCatalogTabsAdapter extends SimpleTabsAdapter {

		public ClassInfoCatalogTabsAdapter(Activity activity, TabHost tabHost, ViewPager pager) {
			super(activity, tabHost, pager);
		}

		@Override
		protected void initPageView(int position) {
			loadList(position);
		}

		@Override
		protected View getPageView(int position) {
			return getTabContentView(position);
		}

	}

	private class ClassInfoListAdapter extends SimpleListAdapter<ClassInfoCatalog> {

		public ClassInfoListAdapter(Context context, ClassInfoCatalog[] data) {
			super(context, data);
		}

		private class ViewHolder {
			TextView	tvTitle;
			TextView	tvDate;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.news_list_item, null);

				holder = new ViewHolder();
				holder.tvTitle = (TextView) convertView.findViewById(R.id.news_catalog_title);
				holder.tvDate = (TextView) convertView.findViewById(R.id.news_catalog_date);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvTitle.setText(mData.get(position).getTitle());
			holder.tvDate.setText(mData.get(position).getDate());
			return convertView;
		}
	}
}